package org.ehe.erp.controller.stock;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.idempotent.annotation.RepeatSubmit;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.erp.domain.stock.bo.ErpStockCheckBo;
import org.ehe.erp.domain.stock.vo.ErpStockCheckVo;
import org.ehe.erp.service.stock.IErpStockCheckService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存盘点单
 * @author :zhangnn
 * @className :ErpStockCheckController
 * @description: TODO
 * @date 2025-08-28 17:41:54
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/stockCheck")
public class ErpStockCheckController extends BaseController {
    private final IErpStockCheckService erpStockCheckService;

    /**
     * 查询ERP 库存盘点单列表
     */
    @SaCheckPermission("erp:stockCheck:list")
    @GetMapping("/list")
    public TableDataInfo<ErpStockCheckVo> list(ErpStockCheckBo bo, PageQuery pageQuery) {
        TableDataInfo<ErpStockCheckVo> erpStockCheckVoTableDataInfo = erpStockCheckService.queryPageList(bo, pageQuery);
        return erpStockCheckVoTableDataInfo;
    }

    /**
     * 导出ERP 库存盘点单列表
     */
    @SaCheckPermission("erp:stockCheck:export")
    @Log(title = "ERP 库存盘点单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpStockCheckBo bo, HttpServletResponse response) {
        List<ErpStockCheckVo> list = erpStockCheckService.queryList(bo);
        ExcelUtil.exportExcel(list, "ERP 库存盘点单", ErpStockCheckVo.class, response);
    }

    /**
     * 获取ERP 库存盘点单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:stockCheck:query")
    @GetMapping("/{id}")
    public R<ErpStockCheckVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return R.ok(erpStockCheckService.queryById(id));
    }

    /**
     * 新增ERP 库存盘点单
     */
    @SaCheckPermission("erp:stockCheck:add")
    @Log(title = "ERP 库存盘点单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpStockCheckBo bo) {
        // 校验盘点单号是否唯一
        if (!erpStockCheckService.isUniqueNo(bo)) {
            return R.fail("盘点单号已存在：" + bo.getNo());
        }
        return toAjax(erpStockCheckService.insertByBo(bo));
    }

    /**
     * 修改ERP 库存盘点单
     */
    @SaCheckPermission("erp:stockCheck:edit")
    @Log(title = "ERP 库存盘点单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpStockCheckBo bo) {
        // 校验盘点单号是否唯一
        if (!erpStockCheckService.isUniqueNo(bo)) {
            return R.fail("盘点单号已存在：" + bo.getNo());
        }
        return toAjax(erpStockCheckService.updateByBo(bo));
    }


}
